package utils;

import book.Book;
import com.bit.utils.FileUtils;
import constant.Constant;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;

public class AnalyzingBook {
    public void storeObject(Book[] books, String filename) throws IOException {
        //有效书籍
        int booksUselen = 0;
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                booksUselen++;
            }
        }

        StringBuilder jsonArray = new StringBuilder();
        for (int i = 0; i < booksUselen; i++) {
            if (books[i] != null) {
                jsonArray.append(books[i].toJson());
                //一本书籍录入以后 以 \n 进行分割
                if (i != booksUselen - 1) {
                    jsonArray.append("\n");
                }
            }
        }
        //把内存当中 存储的数据 写到磁盘上
        FileUtils.writeFile(jsonArray.toString(),filename);
    }
    //Book对象 1. 解析出来一个一个的Book对象
    public Book[] loadObject(String filename) throws IOException{
        //从磁盘的文件当中读取数据，读到内存当中
        String content = FileUtils.readFile(filename);
        if (content == null || content.isEmpty()) {
            System.out.println("图书馆为空！");
            return null;
        }
        String[] bookJsonStrings = content.split("\n");
        Book[] bookList = new Book[bookJsonStrings.length];
        for (int i = 0; i < bookList.length; i++) {
            Book book = parseBookJson(bookJsonStrings[i]);
            bookList[i] = book;
        }
        return bookList;
    }

    private Book parseBookJson(String json) {
        String[] ret = json.split(",");
        int bookID = Integer.parseInt(ret[0]);
        String title = ret[1];
        String author = ret[2];
        String category = ret[3];
        int publishYear = Integer.parseInt(ret[4]);
        boolean isBorrowed = Boolean.parseBoolean(ret[5]);
        int borrowedCount = Integer.parseInt(ret[6]);
        LocalDate shelfDate = LocalDate.parse(ret[7]);
        if (title != null && author != null && category != null && shelfDate != null) {
            Book book = new Book(title, author, category, publishYear, shelfDate);
            book.setBookID(bookID);
            book.setBorrowed(isBorrowed);
            book.setBorrowedCount(borrowedCount);
            return book;
        }
        return null;
    }

    public static void main1(String[] args) {
        Book[] books = new Book[7];
        books[0] = new Book("重生之将门毒后", "千山茶客", "言情", 2013, LocalDate.of(2023,8,12));
        books[1] = new Book("嫡嫁千金", "千山茶客", "言情", 2013, LocalDate.of(2024,11,23));
        books[2] = new Book("灯花笑","千山茶客", "言情", 2024,LocalDate.of(2025,6,8));
        books[3] = new Book("庶女有毒","秦简", "言情",2013,LocalDate.of(2025,7,13));

        AnalyzingBook analyzingBook = new AnalyzingBook();
        try {
            analyzingBook.storeObject(books, Constant.All_BOOK_FILE_NAME);
            Book[] ret = analyzingBook.loadObject(Constant.All_BOOK_FILE_NAME);

            for (int i = 0; i < ret.length; i++) {
                System.out.println(ret[i]);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
